package city.spring.modules.cardinfo.service.impl;

import city.spring.modules.cardinfo.entity.CardMealTimeMoneyEntity;
import city.spring.modules.cardinfo.entity.CardPosEntity;
import city.spring.modules.cardinfo.repository.CardPosRepository;
import city.spring.modules.cardinfo.service.CardMealTimeMoneyService;
import city.spring.modules.cardinfo.service.CardPosService;
import city.spring.modules.ext.EntityExtUtils;
import city.spring.modules.ext.entity.CardPosMealExt;
import city.spring.modules.ext.service.CardPosMealExtService;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Service：消费机
 *
 * @author Panxm
 * @date 2020-04-14 09:34:21
 */
@CacheConfig(cacheNames = {CardPosServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class CardPosServiceImpl extends ServiceImpl<CardPosRepository, CardPosEntity> implements CardPosService {

    private final CardPosMealExtService cardPosMealExtService;
    private final CardMealTimeMoneyService cardMealTimeMoneyService;

    public CardPosServiceImpl(CardPosMealExtService cardPosMealExtService, CardMealTimeMoneyService cardMealTimeMoneyService) {
        this.cardPosMealExtService = cardPosMealExtService;
        this.cardMealTimeMoneyService = cardMealTimeMoneyService;
    }

    @Override
    public void loadMeals(CardPosEntity entity) {
        LambdaQueryChainWrapper<CardPosMealExt> wrapper = cardPosMealExtService.lambdaQuery()
                .eq(CardPosMealExt::getPosId, entity.getId());
        // 获取当前对象关联的员工ID列表信息
        List<String> mealIds = wrapper.list().stream().map(CardPosMealExt::getMealId).collect(Collectors.toList());
        // 从数据库中查出这些员工ID的员工信息
        List<CardMealTimeMoneyEntity> mealEntities = cardMealTimeMoneyService.lambdaQuery()
                .in(CardMealTimeMoneyEntity::getId, mealIds)
                .list();
        entity.setMeals(mealEntities);
    }

    @Override
    public void saveCardPos(CardPosEntity entity) {
        CardPosEntity findEntity = lambdaQuery().eq(CardPosEntity::getName, entity.getName()).one();
        if (findEntity != null) {
            throw new RuntimeException("已经存在该pos机名称");
        }
        save(entity);
        setPosMeal(entity);
    }

    @Override
    public void setPosMeal(CardPosEntity entity) {
        // 删除旧的 培训-培训人员 关联关系  后 再  添加新的关联关系
        EntityExtUtils.repairRelation(cardPosMealExtService, entity,
                CardPosEntity::getId, CardPosEntity::getMeals, CardMealTimeMoneyEntity::getId,
                CardPosMealExt::new, CardPosMealExt::getPosId);

    }

    @Override
    public void updateCardPos(CardPosEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteCardPos(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteCardPos(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}